package com.tt.dp.iterator;

import com.tt.dp.iterator.model.MenuItem;

import java.util.Iterator;

public class DinerMenu implements Menu<MenuItem>{

    private static final int MAX_ITEMS = 6;

    private int numberOfItems = 0;

    private MenuItem[] items;

    public DinerMenu() {

        items = new MenuItem[MAX_ITEMS];

        addItem("Hot dog", "a hot dog ,with saurkraut , relish , onions , topped with cheese", false, 3.05);
    }

    public void addItem(String name, String description, boolean vegetarian, double price) {

        if (numberOfItems > MAX_ITEMS) {
            System.err.println("Sorry , menu is full! Can't add item to menu");
        }

        MenuItem item = new MenuItem(name, description, vegetarian, price);

        items[numberOfItems] = item;

        numberOfItems += 1;
    }

    public MenuItem[] getItems() {
        return items;
    }

    public Iterator<MenuItem> createIterator(){
        return new DinerMenuIterator(items);
    }

    private class DinerMenuIterator implements Iterator<MenuItem> {

        private int position = 0;

        private MenuItem[] items;

        private DinerMenuIterator(MenuItem[] items) {
            this.items = items;
        }

        public boolean hasNext() {
            return position < items.length - 1 || items[position] != null;
        }

        public MenuItem next() {

            MenuItem item = items[position];

            position += 1;

            return item;
        }

        public void remove() {

        }
    }
}
